/**
 * 日志告警控制器
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { 
  Controller, 
  Get, 
  Post, 
  Body, 
  Delete, 
  Param,
  UseGuards,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { 
  ApiTags, 
  ApiOperation, 
  ApiResponse,
  ApiParam,
  ApiBody,
} from '@nestjs/swagger';
import { LogAlertService } from './log-alert.service';
import { LogAlertConfigDto } from './dto/log-alert-config.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RequirePermissions } from '../auth/decorators/permissions.decorator';
import { Log as LogDecorator } from '../../common/decorators/log.decorator';
import { LogType } from '../../interfaces/log.interface';

@ApiTags('日志告警')
@UseGuards(JwtAuthGuard)
@Controller('log-alert')
export class LogAlertController {
  constructor(private readonly logAlertService: LogAlertService) {}

  @Post('config')
  @HttpCode(HttpStatus.CREATED)
  @RequirePermissions('log:alert:config')
  @LogDecorator({
    type: LogType.OPERATION,
    module: '日志告警',
    action: '配置告警',
    description: '配置日志告警规则',
  })
  @ApiOperation({ summary: '添加告警配置' })
  @ApiBody({ type: LogAlertConfigDto })
  @ApiResponse({ 
    status: HttpStatus.CREATED, 
    description: '告警配置添加成功' 
  })
  addAlertConfig(@Body() config: LogAlertConfigDto) {
    this.logAlertService.addAlertConfig(config);
    return { message: '告警配置添加成功' };
  }

  @Get('configs')
  @RequirePermissions('log:alert:read')
  @LogDecorator({
    type: LogType.OPERATION,
    module: '日志告警',
    action: '查询配置',
    description: '查询日志告警配置',
  })
  @ApiOperation({ summary: '获取所有告警配置' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取告警配置成功',
    type: [LogAlertConfigDto],
  })
  getAlertConfigs() {
    return this.logAlertService.getAlertConfigs();
  }

  @Delete('config/:name')
  @HttpCode(HttpStatus.NO_CONTENT)
  @RequirePermissions('log:alert:delete')
  @LogDecorator({
    type: LogType.OPERATION,
    module: '日志告警',
    action: '删除配置',
    description: '删除日志告警配置',
  })
  @ApiOperation({ summary: '删除告警配置' })
  @ApiParam({ name: 'name', description: '告警配置名称' })
  @ApiResponse({ 
    status: HttpStatus.NO_CONTENT, 
    description: '告警配置删除成功' 
  })
  removeAlertConfig(@Param('name') name: string) {
    this.logAlertService.removeAlertConfig(name);
    return { message: '告警配置删除成功' };
  }

  @Post('test')
  @RequirePermissions('log:alert:test')
  @LogDecorator({
    type: LogType.OPERATION,
    module: '日志告警',
    action: '测试告警',
    description: '测试日志告警配置',
  })
  @ApiOperation({ summary: '测试告警配置' })
  @ApiBody({ type: LogAlertConfigDto })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '告警测试成功' 
  })
  async testAlert(@Body() config: LogAlertConfigDto) {
    await this.logAlertService.testAlert(config);
    return { message: '告警测试成功' };
  }
} 